
#include <Arduino.h>
#include <U8g2lib.h>

#ifdef U8X8_HAVE_HW_SPI
#include <SPI.h>
#endif
#ifdef U8X8_HAVE_HW_I2C
#endif

char DisplayText[] = "你好世界朴树妈妈";
char buffer1 [32];
char buffer2 [32];
char buffer3 [32];
char TempBuffer [32];
word brightness = 0;

U8G2_SSD1306_128X64_NONAME_F_SW_I2C u8g2(U8G2_R0, /* clock=*/ PB6, /* data=*/ PB7, /* reset=*/ U8X8_PIN_NONE);
void setup(void) {
  u8g2.begin();
  u8g2.enableUTF8Print();    // enable UTF8 support for the Arduino print() function
  Serial.begin(115200);
}

void drawScrollText(int y,int offset, int delaytime, const char *s) {
  int i = 0;
  while (1) {
    u8g2.setFont(u8g2_font_unifont_t_chinese2);  // use chinese2 for all the glyphs of "你好世界"
    u8g2.setFontDirection(0);
    u8g2.clearBuffer();
    u8g2.setCursor(-i * offset, y);
    i = i + 1;
    u8g2.print(s);   // Chinese "Hello World"
    u8g2.sendBuffer();

    delay(delaytime);
  }
}
//int i = 0;
void loop(void) {
  u8g2.setFont(u8g2_font_unifont_t_chinese1);  // use chinese2 for all the glyphs of "你好世界"
  // 有的中文字符显示不了可能是 这个字体库没有
  u8g2.setFontDirection(0);
  u8g2.clearBuffer();
  u8g2.setCursor(0, 15);
  u8g2.print(buffer1);
  u8g2.setCursor(0, 35);
  u8g2.print(buffer2);
  u8g2.setCursor(0, 55);
//   u8g2.setCursor(-i*4, 55);
  u8g2.print(buffer3);
  u8g2.sendBuffer();
  //drawScrollText(40,2, 100,buffer3);

  if (Serial.available() > 0) {
    // 清空buffer
    memset(TempBuffer, 0, 32 * sizeof(char));
    int responsedatalength = Serial.readBytes(TempBuffer, 32);
    if (TempBuffer[0] == 0x31) {
      for (int i = 0; i < 30; i++) {
        buffer1[i] = TempBuffer[i + 1]; //把x的第i个元素赋值给a1的第i个元素
      }
    }
    if (TempBuffer[0] == 0x32) {
      for (int i = 0; i < 30; i++) {
        buffer2[i] = TempBuffer[i + 1]; //把x的第i个元素赋值给a1的第i个元素
      }
    }
    if (TempBuffer[0] == 0x33) {
      for (int i = 0; i < 30; i++) {
        buffer3[i] = TempBuffer[i + 1]; //把x的第i个元素赋值给a1的第i个元素
      }
    }
    
  }
  delay(100);
}
